#!/bin/bash

# Path to the folder containing mp4 files here
files="Second_half/*.mp4"
outputFolder="Second_half_output"

mkdir -p $outputFolder 
cnt=28

for f in $files
do
    echo $f
    duration=$(ffmpeg -i "$f" 2>&1 | grep "Duration"| cut -d ' ' -f 4 | sed s/,// | awk '{ split($1, A, ":"); print 3600*A[1] + 60*A[2] + A[3] }')
    duration=${duration/.*}
    let endTime=$duration-180
    let startTime=$RANDOM%$endTime
    let cnt=$cnt+1
    ffmpeg -i "$f" -vcodec copy -acodec copy -loglevel quiet -ss $startTime -t 180 "$outputFolder/anime-$cnt.mp4"
    wait
done
